package main

import (
	"encoding/json"
	"fmt"
)

// xiaorui.cc

type Nodes struct {
	Order string `json:"order"`
	Attribute  string `json:"attribute"`
	DirName string `json:"dir_name"`
	BookFileId  string `json:"book_file_id"`
	Children []Nodes `json:"children"`
}


func  main()  {
	//	m := make(map[string]interface{}, 4)





	var  f1 Nodes
	f1.Order="2"
	f1.Attribute="file"
	f1.BookFileId="45454454545"

	var f2 Nodes
	f2.Order="3"
	f2.Attribute="file"
	f2.BookFileId="45454454545"

	var f3 Nodes
	f3.Order="4"
	f3.Attribute="file"
	f3.BookFileId="45454454545"

	var childrens []Nodes
	childrens= append(append(childrens, f1), f2)

	var father1 Nodes
	father1.Order="1"
	father1.Attribute="folder"
	father1.DirName="第一层文件名称"
	father1.Children=childrens


	var father2 Nodes
	father2.Order="5"
	father2.Attribute="folder"
	father2.DirName="第2.1层文件名称"



	var father3 Nodes
	father3.Order="6"
	father3.Attribute="folder"
	father3.DirName="第2.2层文件名称"


	var f3son Nodes
	f3son.Order="7"
	f3son.Attribute="file"
	f3son.BookFileId="45454454545"

	var childrens2 []Nodes
	childrens2= append(childrens2, father3)

	var childrens3 []Nodes
	childrens3= append(childrens3, f3son)

	father2.Children=childrens2

	father3.Children=childrens3

	fmt.Println("=====================")



	//定义 一个 可以包含任意 数据类型的 切片
	s := make([]Nodes, 0)
	s=append(s,father1)
	s=append(s,f3)
	s=append(s,father2)

	fmt.Println("=========",s)


	// map转 json
	result2, err2 := json.Marshal(s)
	if err2 != nil {
		return
	}
	fmt.Println("result2 = ", string(result2))

	//json str 转map

	var jsonStr2 string="[{\"order\":\"1\",\"attribute\":\"folder\",\"dir_name\":\"第一层文件名称\",\"book_file_id\":\"\",\"children\":[{\"order\":\"2\",\"attribute\":\"file\",\"dir_name\":\"\",\"book_file_id\":\"45454454545\",\"children\":null},{\"order\":\"3\",\"attribute\":\"file\",\"dir_name\":\"\",\"book_file_id\":\"45454454545\",\"children\":null}]},{\"order\":\"4\",\"attribute\":\"file\",\"dir_name\":\"\",\"book_file_id\":\"45454454545\",\"children\":null},{\"order\":\"5\",\"attribute\":\"folder\",\"dir_name\":\"第2.1层文件名称\",\"book_file_id\":\"\",\"children\":[{\"order\":\"6\",\"attribute\":\"folder\",\"dir_name\":\"第2.2层文件名称\",\"book_file_id\":\"\",\"children\":null}]}]"
	var dat []Nodes
	if err := json.Unmarshal([]byte(jsonStr2), &dat); err == nil {
		fmt.Println("==============复杂json 转map=======================")
		fmt.Println(dat)
		//切片的长度 和容量
		fmt.Println("切片的长度 和容量")
		fmt.Println(len(dat)) //3 10 容量一般到 末尾
	}


	//v1, exist := dat["order"]
	//if false == exist {
	//	fmt.Println("ddddddddddddd  v1",v1)
	//}

	//循环
	for k, v := range s {
		//fmt.Printf("person[%d]: %s\n", k, v)
		fmt.Println(k,"==========",v)
		//fmt.Println(string(v.["Order"]))
		fmt.Println("=====打印v的部分值=",v.BookFileId)
	}





}
